﻿using db_manage.model;
using db_manage.model.db;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Reflection;
using System.Text;

namespace db_manage.dll.db
{
    /// <summary>
    /// 数据库工厂
    /// </summary>
    public class db_factory : db_base
    {
        private static db_factory instance = new db_factory();
        private db_factory() { }
        public static db_factory Instance
        {
            get
            {
                return instance;
            }
        }

        private static readonly Dictionary<db_type, db_base> db_ibase_list = new Dictionary<db_type, db_base>();
        private db_base db_logic_get(db_link_info db_Link_Info)
        {
            if (!db_ibase_list.ContainsKey(db_Link_Info.type))
            {
                lock (db_ibase_list)
                {
                    if (!db_ibase_list.ContainsKey(db_Link_Info.type))
                    {
                        var type = typeof(db_base);
                        //尝试根据单据类型获取对应类型;/不存在时，用通用类型来替代
                        var target = type.Assembly.GetType(type.Namespace + ".db_" + db_Link_Info.type.ToString(), false, true) ?? type;
                        //实例化
                        db_ibase_list.Add(db_Link_Info.type, target.InvokeMember(null, BindingFlags.CreateInstance, null, null, null) as db_base);
                    }

                }
            }
            return db_ibase_list[db_Link_Info.type];
        }


        public override void link_test(db_link_info db_link_info)
        {
            db_logic_get(db_link_info).link_test(db_link_info);
        }

        public override List<db_info> db_info_list(db_link_info db_link_info)
        {
            return db_logic_get(db_link_info).db_info_list(db_link_info);
        }

        public override void db_record_create(db_info db_info)
        {
            db_logic_get(db_info.db_link_info).db_record_create(db_info);
        }

        public override void detach_move(db_info db_info, string path)
        {
            db_logic_get(db_info.db_link_info).detach_move(db_info, path);
        }

        public override void attach(db_info db_info, string path)
        {
            db_logic_get(db_info.db_link_info).attach(db_info, path);
        }

        public override string bak(db_info db_info, db_bak_param db_bak_param)
        {
            return db_logic_get(db_info.db_link_info).bak(db_info, db_bak_param);
        }

        public override List<db_diff_info> diff_check(db_struct template_db_struct, db_info db_info)
        {
            return db_logic_get(db_info.db_link_info).diff_check(template_db_struct, db_info);
        }

        public override void diff_fix(db_info db_info, List<db_diff_info> list)
        {
            db_logic_get(db_info.db_link_info).diff_fix(db_info, list);
        }

        public override DbConnection db_connection_get(db_info db_info)
        {
            return db_logic_get(db_info.db_link_info).db_connection_get(db_info);
        }

        public override db_struct db_struct_get(db_info db_info, bool is_sql = false)
        {
            return db_logic_get(db_info.db_link_info).db_struct_get(db_info, is_sql);
        }
    }
}
